<?php
namespace app\index\controller;
use think\Config;
use think\Db;

class Index extends Common
{
	
	//首页
    public function index()
    {
		//$time=time();
		//echo "初始时间".date("Y-m-d H:i:s",$time)."</br>";
		
		$baseurl=Config::get("block_url");
		$url = $baseurl . "v1/index/block/preview";
		
		$info = $this->curl_request($url);
		$cinfo = json_decode($info,true);
		$index_data = $cinfo['data'];
		
		$max_power = $index_data['miner_power'];
		$index_data['miner_power'] = $this->chk_power($index_data['miner_power']);
		
		$index_data['24h_reward_per_byte'] = round($index_data['24h_reward_per_byte'],8);
		
		if($index_data['last_block_time']>1000)
		{
			$index_data['block_time'] = 4;
		}
		
		
		//$index_data['gas_price'] = $index_data['gas_price'] / 100000;
		if(empty($index_data['pleg_number_per_tib']))
		{
			$index_data['pleg_number_per_tib'] = 0;
		}
		else
		{
			//$index_data['pleg_number_per_tib'] = $this->chk_num($index_data['pleg_number_per_tib'],5);
		}
		
		//真实矿工
		$url = $baseurl . "v1/index/miner/list";
		$arr['page']=1;
		$info = $this->curl_request($url,$arr);
		$cinfo = json_decode($info,true);
		$level_data = $cinfo['data'];
		
		$level_data = $this->u_array_sort($level_data,'QualityAdjPower','desc');
		$i=0;
		$series="";
		foreach($level_data AS &$v)
		{
			$i++;
			$v['i']=$i;
			$v['width'] = 0;
			if($v['QualityAdjPower']>0)
			{
				$v['width'] = round($v['QualityAdjPower']/$max_power,2) * 100;
			}
			
			$v['QualityAdjPower'] = $this->chk_power($v['QualityAdjPower']);
			
			$v['amount']=$this->sctonum($v['amount'] / 1000000000000000000);
			
			if(empty($v['area']))
			{
				$v['area'] = "N/A";
			}
			
			if($i<=5)
			{
				$series.="{value: ".($v['width']/2).", name: '".$v['miner']."(".$v['tag'].")'},";
			}
		}
		unset($v);
		
		$series.="{value: 50, name: 'Others'}";
		
		
		//var_dump($level_info);exit;
		
		//最新区块
		$url = $baseurl . "v1/index/block/list";
		$arr['page']=1;
		
		$info = $this->curl_request($url,$arr);
		$cinfo = json_decode($info,true);
		$data = $cinfo['data']['data'];
		if(!empty($cinfo))
		{
			foreach($data AS &$v)
			{
				foreach($v AS &$c)
				{
					$c['timestamp']=date("Y-m-d H:i:s",$c['timestamp']);
				}
				unset($c);
			}
			unset($v);
		}
		
		//24H手续费
		$url = $baseurl . "v1/index/gas/24";
		$info = $this->curl_request($url);
		$info = json_decode($info,true);
		$carr = array();
		$str1="";
		$str2="";
		
		//var_dump($info);exit;
		
		foreach($info['data'] AS $v)
		{
			$time = date("H:i:s",$v['time']);
			$price = $v['price']/100000;
			
			//$price = rand(1,10)/10;
			
			$str1.="'{$time}',";
			$str2.="{$price},";
		}
		$str1=substr($str1,0,-1);
		$str2=substr($str2,0,-1);
		
		
		//图表
		$this->assign("str1",$str1);
		$this->assign("str2",$str2);
		
		$this->assign("series",$series);
		
		$this->assign("data",$index_data);
		
		
		
		$this->assign("level_data",$level_data);
		$this->assign("block_data",$data);
		
		
		//$time2=time();
		//echo "结束时间".date("Y-m-d H:i:s",$time2)."</br>";
		
		return $this->fetch();
    }
	
	public function change_lng()
	{
		$lang = request()->param('lang');
		
		$_SESSION['lang'] = $lang;
		
		echo $lang;
	}
	
	
	//搜索
	public function search()
	{
		$content=request()->param('content');
		
		//空数值返回
		if(empty($content))
		{
			$this->redirect("Index");
		}
		
		//纯数字 高度
		if(is_numeric($content))
		{
			$this->redirect("Tipset/Detail",['id'=>$content]);
		}
		
		//62位  消息
		if(strlen($content)==12)
		{
			$this->redirect("Block/Index",['cid'=>$content]);
		}
		
		
		//86位 8位 地址 
		if(strlen($content)==86 | strlen($content)==8)
		{
			$this->redirect("Address/index",['address'=>$content]);
		}
		
		
		
		$this->redirect("Index");
		exit;
		
		$this->assign("str","请输入正确的区块,账户或hash");
		return $this->fetch('index/error');
	}
	
}
